package com.xingshi.springbootinit.controller;

import cn.hutool.core.io.FileUtil;
import com.xingshi.springbootinit.common.BaseResponse;
import com.xingshi.springbootinit.common.ErrorCode;
import com.xingshi.springbootinit.common.ResultUtils;
import com.xingshi.springbootinit.constant.FileConstant;
import com.xingshi.springbootinit.exception.BusinessException;
import com.xingshi.springbootinit.manager.CosManager;
import com.xingshi.springbootinit.model.dto.file.UploadFileRequest;
import com.xingshi.springbootinit.model.entity.User;
import com.xingshi.springbootinit.model.enums.FileUploadBizEnum;
import com.xingshi.springbootinit.service.ScoreService;
import com.xingshi.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Arrays;

/**
 * 积分接口
 * @author 程序员苏打水
 */
@RestController
@RequestMapping("/score")
@Slf4j
public class ScoreController {

    @Resource
    private UserService userService;

    @Resource
    private ScoreService scoreService;

    /**
     * 用于签到时添加积分
     * @param request
     * @return
     */
    @PostMapping("/checkIn")
    public BaseResponse<String> checkIn(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        scoreService.checkIn(loginUser.getId());
        return ResultUtils.success("签到成功");
    }

    /**
     * 查询积分
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Long> getScores(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long totalScores = scoreService.getUserScores(loginUser.getId());
        return ResultUtils.success(totalScores);
    }

    /**
     * 查询签到状态
     *
     * @param request
     * @return
     */
    @GetMapping("/getSign")
    public BaseResponse<Integer> getSignById(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        int isSign = scoreService.getIsSign(loginUser.getId());
        return ResultUtils.success(isSign);
    }
}
